package Servlet;

import Bean.Student;
import Utility.Utility;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/stu")
public class StudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ArrayList<Student> stuList = new ArrayList<>();
        String sql = "select * from student";
        ResultSet rs = Utility.query(sql);
        try {
            while (rs.next()){
                String id = rs.getString("id");
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String sex = rs.getString("sex");
                String address = rs.getString("address");
                Student stu = new Student(id,name,age,sex,address);
                stuList.add(stu);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        req.setAttribute("student",stuList);
        req.getRequestDispatcher("/test.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
